const userDao = require('../dao/user');

const Response = require('../entity/http/Response');

const userService = {
    async getSelfInfo(reqId, uid) {
        let result;
        try {
            let dbResult = await userDao.getUserInfoAndPassword(reqId, uid);
            if (dbResult.length) {
                result = new Response().success(dbResult[0]);
            } else {
                result = new Response().fail(500, "获取当前账号信息失败");
            }
        } catch (err) {
            result = new Response().fail(500, "获取当前账号信息时服务器错误");
        }
        return result;
    },
    async getUserInfo(reqId, uid, { id: userId }) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let selfUserType, userType;
            let queryResult1 = await userDao.getUserType(reqId, uid);
            let queryResult2 = await userDao.getUserType(reqId, userId);
            if (queryResult1.length) {
                selfUserType = queryResult1[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (queryResult2.length) {
                userType = queryResult2[0].userType;
            } else {
                return new Response().fail(500, "不存在该用户");
            }
            if (selfUserType != 'root' && [ 'root', 'sub-root' ].includes(userType)) {
                return new Response().fail(403, "权限不足");
            }
            // 获取用户信息
            let dbResult = await userDao.getUserInfo(reqId, userId);
            if (dbResult.length) {
                result = new Response().success(dbResult[0]);
            } else {
                result = new Response().fail(500, "获取用户信息失败");
            }
        } catch (err) {
            result = new Response().fail(500, "获取用户信息时服务器错误");
        }
        return result;
    },
    async getUserInfoAndPassword(reqId, uid, { id: userId }) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let selfUserType, userType;
            let queryResult1 = await userDao.getUserType(reqId, uid);
            let queryResult2 = await userDao.getUserType(reqId, userId);
            if (queryResult1.length) {
                selfUserType = queryResult1[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (queryResult2.length) {
                userType = queryResult2[0].userType;
            } else {
                return new Response().fail(500, "不存在该用户");
            }
            if (selfUserType != 'root' && [ 'root', 'sub-root' ].includes(userType)) {
                return new Response().fail(403, "权限不足");
            }
            // 获取用户信息以及密码
            let dbResult = await userDao.getUserInfoAndPassword(reqId, userId);
            if (dbResult.length) {
                result = new Response().success(dbResult[0]);
            } else {
                result = new Response().fail(500, "获取用户信息失败");
            }
        } catch (err) {
            result = new Response().fail(500, "获取用户信息时服务器错误");
        }
        return result;
    },
    async register(reqId, uid, { userType, name, phone, account, password }) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let selfUserType;
            let queryResult = await userDao.getUserType(reqId, uid);
            if (queryResult.length) {
                selfUserType = queryResult[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (selfUserType != 'root' && [ 'root', 'sub-root' ].includes(userType)) {
                return new Response().fail(403, "权限不足");
            }
            // 注册用户的逻辑
            let dbResult = await userDao.addUser(reqId, userType, name, phone, account, password);
            if (dbResult.insertId) {
                result = new Response().success("注册用户成功");
            } else {
                result = new Response().fail(500, "注册用户失败");
            }
        } catch (err) {
            result = new Response().fail(500, "注册用户时服务器错误");
        }
        return result;
    },
    async getPower(reqId, uid) {
        let result;
        try {
            let dbResult = await userDao.getPower(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取用户权限范围时服务器错误");
        }
        return result;
    },
    async getAllUser(reqId, uid) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let daoFunc;
            let selfUserType;
            let queryResult = await userDao.getUserType(reqId, uid);
            if (queryResult.length) {
                selfUserType = queryResult[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (selfUserType != 'root') {
                daoFunc = userDao.getAllUser_usePower;
            } else {
                daoFunc = userDao.getAllUser;
            }
            // 查看所有用户
            let dbResult = await daoFunc(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取全量用户信息时服务器错误");
        }
        return result;
    },
    async getAllUserAndPassword(reqId, uid) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let daoFunc;
            let selfUserType;
            let queryResult = await userDao.getUserType(reqId, uid);
            if (queryResult.length) {
                selfUserType = queryResult[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (selfUserType != 'root') {
                daoFunc = userDao.getAllUserAndPassword_usePower;
            } else {
                daoFunc = userDao.getAllUserAndPassword;
            }
            // 查看所有用户以及密码的逻辑
            let dbResult = await daoFunc(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取全量用户信息时服务器错误");
        }
        return result;
    },
    async deleteUser(reqId, uid, userId) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let selfUserType, userType;
            let queryResult1 = await userDao.getUserType(reqId, uid);
            let queryResult2 = await userDao.getUserType(reqId, userId);
            if (queryResult1.length) {
                selfUserType = queryResult1[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (queryResult2.length) {
                userType = queryResult2[0].userType;
            } else {
                return new Response().fail(500, "不存在该用户");
            }
            if (selfUserType != 'root' && [ 'root', 'sub-root' ].includes(userType)) {
                return new Response().fail(403, "权限不足");
            }
            // 删除用户的逻辑
            let dbResult = await userDao.deleteUser(reqId, uid, userId);
            if (dbResult.changedRows) {
                result = new Response().success("删除用户成功");
            } else {
                result = new Response().fail(500, "不存在该用户");
            }
        } catch (err) {
            result = new Response().fail(500, err.message);
        }
        return result;
    },
    async updateUserInfo(reqId, uid, { id, name, phone }) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let selfUserType, userType;
            let queryResult1 = await userDao.getUserType(reqId, uid);
            let queryResult2 = await userDao.getUserType(reqId, id);
            if (queryResult1.length) {
                selfUserType = queryResult1[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (queryResult2.length) {
                userType = queryResult2[0].userType;
            } else {
                return new Response().fail(500, "不存在该用户");
            }
            if (selfUserType != 'root' && [ 'root', 'sub-root' ].includes(userType)) {
                return new Response().fail(403, "权限不足");
            }
            // 更新用户信息的逻辑
            let dbResult = await userDao.updateUserInfo(reqId, uid, id, name, phone);
            if (dbResult.changedRows) {
                result = new Response().success("更新用户信息成功");
            } else {
                result = new Response().fail(500, "更新用户信息无变化");
            }
        } catch (err) {
            result = new Response().fail(500, "修改用户信息时服务器错误");
        }
        return result;
    },
    async updateUserInfoAndPassword(reqId, uid, { id, name, phone, password }) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let selfUserType, userType;
            let queryResult1 = await userDao.getUserType(reqId, uid);
            let queryResult2 = await userDao.getUserType(reqId, id);
            if (queryResult1.length) {
                selfUserType = queryResult1[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (queryResult2.length) {
                userType = queryResult2[0].userType;
            } else {
                return new Response().fail(500, "不存在该用户");
            }
            if (selfUserType != 'root' && [ 'root', 'sub-root' ].includes(userType)) {
                return new Response().fail(403, "权限不足");
            }
            // 更新用户信息的逻辑
            let dbResult = await userDao.updateUserInfoAndPassword(reqId, uid, id, name, phone, password);
            if (dbResult.changedRows) {
                result = new Response().success("更新用户信息成功");
            } else {
                result = new Response().fail(500, "更新用户信息无变化");
            }
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "修改用户信息时服务器错误");
        }
        return result;
    },
    async updateSelfInfoAndPassword(reqId, uid, { name, phone, password }) {
        let result;
        try {
            let dbResult = await userDao.updateSelfInfoAndPassword(reqId, uid, name, phone, password);
            if (dbResult.changedRows) {
                result = new Response().success("更新用户信息成功");
            } else {
                result = new Response().fail(500, "更新用户信息无变化");
            }
        } catch (err) {
            result = new Response().fail(500, "修改用户信息时服务器错误");
        }
        return result;
    },
    async getUserDepartment(reqId, uid, { id: userId }) {
        let result;
        try {
            let dbResult = await userDao.getUserDepartment(reqId, uid, userId);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取用户负责的科室列表时服务器错误");
        }
        return result;
    },
    async getSelfDepartment(reqId, uid) {
        let result;
        try {
            let dbResult = await userDao.getUserDepartment(reqId, uid, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取用户负责的科室列表时服务器错误");
        }
        return result;
    },
    async getSelfPowerList(reqId, uid) {
        let result;
        try {
            let dbResult1 = userDao.getUserType(reqId, uid);
            let dbResult2 = userDao.getSelfPowerList(reqId, uid);
            let res = await Promise.all([dbResult1, dbResult2]);
            let ret = {
                userType: res[0][0].userType,
                powerList: res[1].map(item => item.power)
            };
            result = new Response().success(ret);
        } catch (err) {
            result = new Response().fail(500, "获取当前用户权限列表时服务器错误");
        }
        return result;
    }
};

module.exports = userService;